package com.example.sample.activity;

import com.example.app.BaseActivity;
import com.example.sample.R;

import android.app.Activity;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import net.liteheaven.xblib.common.ui.DimensionUtil;

public class TextViewTransformActivity extends BaseActivity {
	
	private static final String TAG = "TextViewTransformActivity";
	private GestureDetector mGestureDetector;
	private TextView tvCenter;
	private TextView tvLt;
	private TextView tvRt;
	private TextView tvLb;
	private TextView tvRb;
	private Point sc;
	
	protected void onCreate(android.os.Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
		this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
		setContentView(R.layout.activity_textview_transform);
		tvCenter = (TextView)findViewById(R.id.tvCenter);
		tvLt = (TextView)findViewById(R.id.tvLt);
		tvRt = (TextView)findViewById(R.id.tvRt);
		tvLb = (TextView)findViewById(R.id.tvLb);
		tvRb = (TextView)findViewById(R.id.tvRb);
		
		sc = DimensionUtil.getScreenSize(this);
		
		// 手势监听
		mGestureDetector = new GestureDetector(this, new GestureListener());
		
		tvLt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onClickLt();
			}
		});
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mGestureDetector.onTouchEvent(event);
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			tvCenter.setTranslationX(0);
			tvCenter.setTranslationY(0);
			tvCenter.setScaleX(1);
			tvCenter.setScaleY(1);
			break;
		default:
			break;
		}
		
		return super.onTouchEvent(event);
	}
	
	private void onClickLt(){
		tvCenter.measure(0, 0);
		float dx = tvCenter.getWidth() / 2f - sc.x / 2f;
		float dy = tvCenter.getHeight() / 2f - sc.y / 2f;
		tvCenter.setTranslationX(dx);
		tvCenter.setTranslationY(dy);
		tvCenter.setScaleX(1);
		tvCenter.setScaleY(1);
	}
	
	protected class GestureListener extends SimpleOnGestureListener{
		// 拖拽起始点
		private PointF mScrollFrom;
		
		@Override
		public boolean onDown(MotionEvent e) {
			Log.i(TAG, "onDown");
			mScrollFrom = new PointF(e.getRawX(), e.getRawY());
			return true;
		}

		@Override
		public void onShowPress(MotionEvent e) {
			Log.i(TAG, "onShowPress");
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			Log.i(TAG, "onSingleTapUp");
			return false;
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
					
			// 计算总共移动距离
			float xInc = e2.getRawX() - e1.getRawX();
			float yInc = e2.getRawY() - e1.getRawY();
			Log.i(TAG, "onScroll " + xInc + "/" + yInc);	
			
			float distance = (float) Math.sqrt(xInc * xInc + yInc * yInc);
			float scale = 1f - distance / 1000f;
			
			tvCenter.setTranslationX(xInc);
			tvCenter.setTranslationY(yInc);
			tvCenter.setScaleX(scale);
			tvCenter.setScaleY(scale);
			
			// 重设位置
			return false;
		}

		@Override
		public void onLongPress(MotionEvent e) {
			Log.i(TAG, "onLongPress");
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			Log.i(TAG, "onFling");
			return false;
		}
		
		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			Log.i(TAG, "onSingleTapConfirmed");
			return super.onSingleTapConfirmed(e);
		}		
	}

}
